# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=tarantool
pkgver=1.7.4.335
_series=${pkgver%.*}; _series=${_series%.*}  # x.y
pkgrel=8
pkgdesc="Lua application server integrated with a database management system"
url="https://tarantool.org"
# x86, aarch64: fails to build
# armhf: zstd not available (fails to build)
# ppc64le: not supported by LuaJIT w/o patches
# s390x: not supported by LuaJIT
arch=""
license="BSD-2-Clause MIT Public-Domain"
pkgusers="$pkgname"
pkggroups="$pkgname"
install="$pkgname.pre-install"
checkdepends="python2-dev py2-argparse py2-gevent py2-msgpack py2-six py2-yaml"
makedepends="cmake curl-dev luajit-dev libcoro-dev openssl-dev msgpuck-dev
	perl python2 readline-dev yaml-dev zstd-dev"
subpackages="$pkgname-dev $pkgname-doc"
source="http://download.tarantool.org/tarantool/$_series/src/$pkgname-$pkgver.tar.gz
	$pkgname.initd
	unbundle-dependencies.patch
	tests-musl-compat.patch
	tests-libressl-compat.patch
	fix-compilation-with-libcurl-7.62.0.patch
	"
builddir="$srcdir/$pkgname-$pkgver"

prepare() {
	default_prepare
	cd "$builddir"

	# XXX: This test doesn't work, probably due to fakeroot.
	rm test/xlog-py/misc.test.py

	# msgpuck.h is in /usr/include, not /usr/include/msgpuck.
	find . -name '*.c' -o -name '*.cc' \
		| xargs -n 1 sed -Ei 's|#include\s*["<]msgpuck/msgpuck\.h[">]|#include <msgpuck.h>|'

	# Remove bundled libraries.
	rm -r src/lib/msgpuck
	cd third_party
	rm -r coro libyaml zstd
}

build() {
	cd "$builddir"

	# Bundled libraries:
	# - libgopt (Public Domain): probably patched, it does not correspond
	#   to 8.1 as stated in header
	# - luajit (MIT): tarantool requires non-exported symbols, so cannot link
	#   dynamically; linking with system static libluajit doesn't work too
	cmake \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DCMAKE_SKIP_RPATH=ON \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DENABLE_BUNDLED_LIBCORO=OFF \
		-DENABLE_BUNDLED_LIBGOPT=ON \
		-DENABLE_BUNDLED_LIBYAML=OFF \
		-DENABLE_BUNDLED_LUAJIT=ON \
		-DENABLE_BUNDLED_MSGPUCK=OFF \
		-DENABLE_BUNDLED_ZSTD=OFF \
		-DENABLE_DIST=ON \
		-DWITH_SYSVINIT=OFF \
		-DWITH_SYSTEMD=OFF
	make VERBOSE=1 -j2  # there's some problem with parallel build
}

check() {
	cd "$builddir"/test

	../src/tarantool --help > /dev/null

	# TODO: Some tests are broken, fix it!
	# --force means that it will not stop after first error.
	#python2 test-run.py --force
}

package() {
	cd "$builddir"

	make DESTDIR="$pkgdir" install
	rm -R "$pkgdir"/etc/default

	install -Dm755 "$srcdir"/tarantool.initd "$pkgdir"/etc/init.d/tarantool
}

sha512sums="c95a6900d9e9b9963bb038f28f3e47d0f8fd66159efef99b98b792cd08748481043d9a2a39c1c455094725e1ac213df731280587b93746ecb7bea2431deb7bf3  tarantool-1.7.4.335.tar.gz
fe463c4b6ab8708ea6ec17bb6a887677ae131a489d2ce299ce951790b7c134ff356bc1a4d4745878beec3600ec702944c2329a3f02645f8ab0a0eb24edb6215a  tarantool.initd
b48eefdf71e20db22c4366e4573441a0d4e5f6e727fc23b2b41534e8d5cc7dc9cc08baf2bf1cf1950c7b0e211920abb4254c2d1e972ec884db25800820824f2c  unbundle-dependencies.patch
f6fec037780f830eb353b184d190b5dc8b481164c9cd06991f921d70be8cababb5dba43e5af96abe6fb87fda44c5573d706f7b032bc996d33debb7811b84addb  tests-musl-compat.patch
146744f2e7265de2644c03d7beb6ce1657b63a99db9c4527cb603c915082dc1184a90e92ba39bfb946f2f8f7a2b866cc8343c7e865d4245fe704447a59cc4846  tests-libressl-compat.patch
db687fee5cc6cc975d560f17abf51a6a258a964ab1322ee5d209a153d98c80263fc36f0ae8e78d480dc9f0fb68d4de1e01174d111eb21e794c89d38934b85b01  fix-compilation-with-libcurl-7.62.0.patch"
